﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SportStore.Domain.Entities;
using SportStore.WebUI.Controllers;
using SportStore.WebUI.Models;

namespace SportStore.UnitTests
{
    [TestFixture]
    public class NavigationByCategory
    {
        [Test]
        public void NavMenu_Includes_Alphabetial_List_Of_Distinct_Categories()
        {
            var mockProductsRepository = UnitTestHelpers.MockProductsRepository(
                new Product { Category = "C2", Name = "P2" },
                new Product { Category = "C1", Name = "P1" },
                new Product { Category = "C4", Name = "P4" },
                new Product { Category = "C5", Name = "P5" },
                new Product { Category = "C3", Name = "P3" },
                new Product { Category = "C5", Name = "P8" }
                );
            var result = new NavController(mockProductsRepository).Menu(null);

            var categoryLinks = ((IEnumerable<NavLink>)result.ViewData.Model)
                                .Where(x => x.RouteValues["category"] != null);

            CollectionAssert.AreEqual(new[] { "C1", "C2", "C3", "C4", "C5" }, categoryLinks.Select(x => x.RouteValues["category"]));
            foreach (var link in categoryLinks)
            {
                link.RouteValues["controller"].ShouldEqual("Products");
                link.RouteValues["action"].ShouldEqual("List");
                link.RouteValues["page"].ShouldEqual(1);
                link.Text.ShouldEqual(link.RouteValues["category"]);
            }
        }

        [Test]
        public void NavMenu_Shows_Home_Link_At_Top()
        {
            var mockProductsRepository = UnitTestHelpers.MockProductsRepository();
            var result = new NavController(mockProductsRepository).Menu(null);
            var topLink = ((IEnumerable<NavLink>)result.ViewData.Model).First();
            topLink.RouteValues["controller"].ShouldEqual("Products");
            topLink.RouteValues["action"].ShouldEqual("List");
            topLink.RouteValues["page"].ShouldEqual(1);
            topLink.Text.ShouldEqual("Home");
        }

        [Test]
        public void NavMenu_Highlights_Current_Category()
        {
            var mockProductsRepository = UnitTestHelpers.MockProductsRepository(
                new Product { Category = "C2", Name = "P2" },
                new Product { Category = "C1", Name = "P1" },
                new Product { Category = "C4", Name = "P4" },
                new Product { Category = "C5", Name = "P5" },
                new Product { Category = "C3", Name = "P3" },
                new Product { Category = "C5", Name = "P8" }
                );
            var result = new NavController(mockProductsRepository).Menu("C2");

            var highLightedLinks = ((IEnumerable<NavLink>)result.ViewData.Model).Where(x => x.IsSelected).ToList();
            highLightedLinks.Count.ShouldEqual(1);
            highLightedLinks[0].Text.ShouldEqual("C2");

        }
    }
}
